<?php

namespace frontend\modules\resource\logic;


use common\models\Branch;
use common\models\HyTheme;
use frontend\modules\common\models\BaseHelper;
use frontend\modules\resource\models\BranchSearch;
use yii\helpers\ArrayHelper;
use yii\web\NotFoundHttpException;


/**
 * BranchSearch represents the model behind the search form of `common\models\Branch`.
 */
class BranchLogic
{

    /**
     * @param $id
     * @return bool
     * @throws \yii\base\ExitException
     */
    public function branchShowHide($id)
    {
        if (!is_numeric($id))
            BaseHelper::Error('id invalid');
        $model = BranchSearch::find()->where(['id' => $id])->one();
        if (empty($model))
            BaseHelper::Error('id invalid');
        $model->show = !$model->show;
        return $model->save(false);
    }

    /**
     * @param $id
     * @return array|null|\yii\db\ActiveRecord
     */
    public function getBranchInfo($id)
    {
        $info = Branch::find()->select('title')->where(['id' => $id])->asArray()->one();
        return $info;
    }

    /**
     * @param int $cateId
     * @return array
     */
    public static function colorMap()
    {
        $res = (new HyTheme())->find()
            ->select('id,title')
            ->asArray()
            ->all();

        $data = ArrayHelper::map($res, 'id', 'title');
        return $data;
    }

}
